﻿/*
 * imaeg - generic image utility in C#
 * Copyright (C) 2010  ed <tripflag@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v2
 * (version 2) as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, refer to the following URL:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace imaeg {
    class Img {
        Image bm;
        string path;
        bool _finished, _failed;
        public Img(string path) {
            this.path = path;
            dispose();
            //Program.dbg("Img instance created");
        }

        /// <summary>
        /// Load image from file
        /// </summary>
        /// <param name="root">Parent folder for instance path</param>
        public void load(string root) {
            Image im = null;
            try {
                Program.dbg("Loading image " + path);
                im = new Bitmap(root + path);
                Program.dbg("Finished without errors.");
            } catch {
                Program.dbg("Loading failed!");
            }
            set(im);
        }
        
        /// <summary>
        /// Load image from file, applying filter filter
        /// </summary>
        /// <param name="root">Parent folder for instance path</param>
        /// <param name="filter">The ImgFilter to pass image through</param>
        public void load(string root, ImgFilter filter) {
            Image im = null;
            try {
                Program.dbg("Loading image " + path);
                Bitmap asdf = new Bitmap(root + path);
                Program.dbg("Applying filters");
                im = filter.apply(asdf);
                Program.dbg("Finished without errors.");
            } catch {
                Program.dbg("Loading failed!");
            }
            set(im);
        }

        /// <summary>
        /// True if (attempted) image loading finished.
        /// False if loading hasn't finished.
        /// </summary>
        public bool finished {
            get { return _finished; }
            set { }
        }

        /// <summary>
        /// True if loading finished with errors.
        /// False if image loaded successfully.
        /// </summary>
        public bool failed {
            get { return _failed; }
            set { }
        }

        /// <summary>
        /// Initialize this instance
        /// </summary>
        public void dispose() {
            if (bm != null) {
                Program.dbg("Disposing " + path);
                bm.Dispose();
                bm = null;
            }
            // All cleared
            _finished = false;
            _failed = false;
        }

        /// <summary>
        /// Return the image (or null)
        /// </summary>
        /// <returns>HURR</returns>
        public Image get() {
            Program.dbg("Image " + path + " (" + (bm == null? "null" : "not null") + ") requested");
            return bm;
        }

        /// <summary>
        /// Manually set new image
        /// </summary>
        /// <param name="im">DURR</param>
        public void set(Image im) {
            dispose();
            bm = im;

            if (im != null) {
                // Set to valid image;
                // nothing wrong in here.
                _finished = true;
                _failed = false;
            } else {
                // Explicitly set to null;
                // assume externa failure
                _finished = true;
                _failed = true;
            }
        }

        /// <summary>
        /// Get relative path of this image
        /// </summary>
        /// <returns>Relative path of image</returns>
        public string getName() {
            return path;
        }

        /// <summary>
        /// Set relative path of this image
        /// </summary>
        /// <param name="s">New relative path of image</param>
        public void setName(string s) {
            Program.dbg("Changed " + path + " to " + s);
            path = s;
        }
    }
}
